import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'liquidityRiskM',
  state: {
    echartsData: { xAxis: [], series: [] },
    tableDataOne: { columns: [], dataSource: [] },
    tableDataTwo: { columns: [], dataSource: [] },
  },

  effects: {
    // 获取无冲击变现天数持仓分布
    *getTableDataOne({ payload, callback }, { call, put }) {
      const res = yield call(api.liquidityRisk.getTableDataOne, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableDataOne: res.data || { columns: [], dataSource: [] },
          },
        });
        // 查询数据为空，如有提示需要提示
        if (res.data?.dataSource.length === 0 && res?.msg) {
          message.info(res.msg);
        }
      } else if (res && res.code == -1) {
        message.error(res.msg);
      } else if (res && res.code == 2) {
        yield put({
          type: 'save',
          payload: {
            tableDataOne: res.data || { columns: [], dataSource: [] },
          },
        });
      }
    },
    // 获取流动性明细
    *getTableDataTwo({ payload, callback }, { call, put }) {
      const res = yield call(api.liquidityRisk.getTableDataTwo, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableDataTwo: res.data || { columns: [], dataSource: [] },
          },
        });
      } else if (res && res.code == -1) {
        message.error(res.msg);
      } else {
        message.error('获取数据失败');
      }
    },
    // 获取echarts数据
    *getEchartsData({ payload, callback }, { call, put }) {
      const res = yield call(api.liquidityRisk.getEchartsData, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            echartsData: res.data,
          },
        });
      } else if (res && res.code == -1) {
        message.error(res.msg);
      } else if (res && res.code == 2) {
        yield put({
          type: 'save',
          payload: {
            echartsData: res.data,
          },
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
